import atomfetch

import urlparse

class Entry(object):
  def __init__(self, entry_node):
    self._entry_node = entry_node

  def _get_tag_content(self, tag):
    return self._entry_node.getElementsByTagName(tag)[0].firstChild.data

  def get_content(self):
    return self._get_tag_content('content')

  def get_title(self):
    return self._get_tag_content('title')

  def get_published(self):
    return self._get_tag_content('published')

  def get_path(self):
    for link in self._entry_node.getElementsByTagName('link'):
      if link.hasAttribute('type'):
        if (link.getAttribute('type') == 'text/html' and
            link.getAttribute('rel') == 'alternate'):
          url = urlparse.urlparse(link.getAttribute('href'))
          return url.path


  
class BlogFetcher(object):
  def __init__(self, blog_id):
    self._blog_id = blog_id

  def get_entries(self):
    docs = atomfetch.fetch_docs(
      'http://www.blogger.com/feeds/%s/posts/default' % self._blog_id)

    entries = []
    for doc in docs:
      entry_nodes = doc.getElementsByTagName('entry')
      for node in entry_nodes:
        entries.append(Entry(node))

    return entries

